package cn.com.gf.cmp.am.monitor.entrust;

import java.util.HashMap;
import java.util.Map;

import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.common.serialization.StringSerializer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.DefaultKafkaProducerFactory;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;

@Configuration
@ConfigurationProperties(prefix = "spring.kafka.amentrust")
public class EntrustKafkaConfig {
	
	private String servers;
    private String topics;
    @Value("${spring.kafka.amentrust.batch.size}")
    private int batchSize;
    @Value("${spring.kafka.amentrust.lineger.ms}")
    private int linegerMS;

	public String getServers() {
		return servers;
	}

	public void setServers(String servers) {
		this.servers = servers;
	}

	public String getTopics() {
		return topics;
	}

	public void setTopics(String topics) {
		this.topics = topics;
	}

	public int getBatchSize() {
		return batchSize;
	}

	public void setBatchSize(int batchSize) {
		this.batchSize = batchSize;
	}

	public int getLinegerMS() {
		return linegerMS;
	}

	public void setLinegerMS(int linegerMS) {
		this.linegerMS = linegerMS;
	}

	@Bean(name="entrustKafkaTemplate")
	public KafkaTemplate<String, String> createTemplate() {
	    Map<String, Object> senderProps = producerConfigs();
	    ProducerFactory<String, String> pf =
	              new DefaultKafkaProducerFactory<String, String>(senderProps);
	    KafkaTemplate<String, String> template = new KafkaTemplate<>(pf);
	    return template;
	}

	@Bean(name="entrustProducerConfigs")
	public Map<String, Object> producerConfigs() {
	    Map<String, Object> props = new HashMap<String, Object>();
	    props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, servers);
	    props.put(ProducerConfig.RETRIES_CONFIG, 0);
	    props.put(ProducerConfig.BATCH_SIZE_CONFIG, batchSize);
	    props.put(ProducerConfig.LINGER_MS_CONFIG, linegerMS);
	    props.put(ProducerConfig.BUFFER_MEMORY_CONFIG, 33554432);
	    props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
	    props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
	    return props;
	}
}
